<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .done {
            text-decoration: line-through;
            color: #999;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="title">
        <button @click="add">添加代办</button>
        <hr />
        <ul>
            <li v-for="item of todo" :key="item.id" :class="{done:item.done}">
                <input type="checkbox" :checked="item.done" @click.prevent="change(item.id,item.done)">
                <span>{{ item.title }}</span>
                <a href="javascript:;" @click="del(item.id)">删除</a>
            </li>
        </ul>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                title: '',
                todo: []
            },
            created() {
                this.getData()
            },
            methods: {
                getData() {
                    fetch('/todo').then(res => {
                        return res.json()
                    }).then(rsp => {
                        this.todo = rsp.data
                    })
                },
                add() {
                    fetch('/add', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ title: this.title })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                        if (rsp.code == 1) return alert(rsp.data)
                        this.getData();
                        this.title = '';
                    })
                },
                del(id) {
                    fetch('/del', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                        if (rsp.code == 1) return alert(rsp.data)
                        this.getData()
                    })
                },
                change(id,done) {
                    fetch('/change', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id, done: Number(!done) })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                        if (rsp.code == 1) return alert(rsp.data)
                        let todo = this.todo.find(item => {
                            return item.id == id
                        })
                        todo.done = !todo.done
                    })
                }
            }
        })
    </script>
</body>

</html>